package cn.qzuie.smart.module.dorm.controller.admin.buildings;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import cn.qzuie.smart.framework.common.pojo.PageParam;
import cn.qzuie.smart.framework.common.pojo.PageResult;
import cn.qzuie.smart.framework.common.pojo.CommonResult;
import cn.qzuie.smart.framework.common.util.object.BeanUtils;
import static cn.qzuie.smart.framework.common.pojo.CommonResult.success;

import cn.qzuie.smart.framework.excel.core.util.ExcelUtils;

import cn.qzuie.smart.framework.apilog.core.annotation.ApiAccessLog;
import static cn.qzuie.smart.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.qzuie.smart.module.dorm.controller.admin.buildings.vo.*;
import cn.qzuie.smart.module.dorm.dal.dataobject.buildings.BuildingsDO;
import cn.qzuie.smart.module.dorm.service.buildings.BuildingsService;

@Tag(name = "管理后台 - 栋")
@RestController
@RequestMapping("/dorm/buildings")
@Validated
public class BuildingsController {

    @Resource
    private BuildingsService buildingsService;

    @PostMapping("/create")
    @Operation(summary = "创建栋")
    @PreAuthorize("@ss.hasPermission('dorm:buildings:create')")
    public CommonResult<Integer> createBuildings(@Valid @RequestBody BuildingsSaveReqVO createReqVO) {
        return success(buildingsService.createBuildings(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新栋")
    @PreAuthorize("@ss.hasPermission('dorm:buildings:update')")
    public CommonResult<Boolean> updateBuildings(@Valid @RequestBody BuildingsSaveReqVO updateReqVO) {
        buildingsService.updateBuildings(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除栋")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dorm:buildings:delete')")
    public CommonResult<Boolean> deleteBuildings(@RequestParam("id") Integer id) {
        buildingsService.deleteBuildings(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除栋")
                @PreAuthorize("@ss.hasPermission('dorm:buildings:delete')")
    public CommonResult<Boolean> deleteBuildingsList(@RequestParam("ids") List<Integer> ids) {
        buildingsService.deleteBuildingsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得栋")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dorm:buildings:query')")
    public CommonResult<BuildingsRespVO> getBuildings(@RequestParam("id") Integer id) {
        BuildingsDO buildings = buildingsService.getBuildings(id);
        return success(BeanUtils.toBean(buildings, BuildingsRespVO.class));
    }
    @GetMapping("/all")
    @Operation(summary = "获得所有栋")
    @PreAuthorize("@ss.hasPermission('dorm:buildings:query')")
    public CommonResult<List<BuildingsRespVO>> getBuildingsAll() {
        List<BuildingsDO> buildingDOs = buildingsService.getBuildingsAll();
        List<BuildingsRespVO> buildingVOs = buildingDOs.stream()
                .map(building -> BeanUtils.toBean(building, BuildingsRespVO.class))
                .collect(Collectors.toList());
        return success(buildingVOs);
    }

    @GetMapping("/page")
    @Operation(summary = "获得栋分页")
    @PreAuthorize("@ss.hasPermission('dorm:buildings:query')")
    public CommonResult<PageResult<BuildingsRespVO>> getBuildingsPage(@Valid BuildingsPageReqVO pageReqVO) {
        PageResult<BuildingsDO> pageResult = buildingsService.getBuildingsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BuildingsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出栋 Excel")
    @PreAuthorize("@ss.hasPermission('dorm:buildings:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBuildingsExcel(@Valid BuildingsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BuildingsDO> list = buildingsService.getBuildingsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "栋.xls", "数据", BuildingsRespVO.class,
                        BeanUtils.toBean(list, BuildingsRespVO.class));
    }

}